package getBaiduPaid;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class KeywordQ {

	public static void Mark (String keyword,int yesno) 
			throws ClassNotFoundException, FileNotFoundException, IOException, SQLException {
	
		Config.stm.executeUpdate("UPDATE "+Config.TableKeywordQ+" SET "+Config.VisitStatucCol+"="+yesno+" WHERE keywords='"+keyword+"'");
	}
	
	public static boolean isVisited (String keyword) 
			throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {

		Config.rs = Config.stm.executeQuery("SELECT * FROM "+Config.TableKeywordQ+" WHERE keywords='"+keyword+"' " +
				"AND visited=1");
		
		if (Config.rs.next()) return true;
		else return false;
	}
	
	public List<String> NextBatch(int batch) 
			throws ClassNotFoundException, SQLException, FileNotFoundException, IOException {
		List<String> nextBatch = new ArrayList<String>();
		Config.Init();
		Config.rs = Config.stm.executeQuery("SELECT keywords FROM "+Config.TableKeywordQ+" WHERE "+Config.VisitStatucCol+"=0 LIMIT "+batch);
		
		while(Config.rs.next()) {
			nextBatch.add(Config.rs.getString("keywords"));
		}
		for (String kw : nextBatch) {
			Mark(kw,1);
		}
		return nextBatch;
	}
	
	public static void Reset () throws SQLException {
		Config.stm.executeUpdate("UPDATE "+Config.TableKeywordQ+" SET "+Config.VisitStatucCol+"=0");
	}

	
	public static void main (String args[]) 
			throws ClassNotFoundException, FileNotFoundException, IOException, SQLException {
//		String keyword = "闲置童装市场";
//		Mark(keyword,0);
		LinkedList<String> kws = new LinkedList<String>();
		KeywordQ q = new KeywordQ();

		for (String s : q.NextBatch(1))
			kws.add(s);
		
		for (String s : kws) System.out.println(s);
	}
}
